import 'package:dio/dio.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';

import 'http_config.dart';

class HttpRequest {
  // 1、创建DIO实例
  static BaseOptions baseOptions =
      BaseOptions(baseUrl: BASE_URL, connectTimeout: TIMEOUT);
  static final dio = Dio(baseOptions);

  // 2、发送76  网络请求
  static Future request(String url,
      {String method = 'get',
      Map<String, dynamic> params,
      dynamic data,
      String token}) async {
    // Options options = Options(method: method, headers: );
    Options options =
        Options(method: method, headers: {"token": token.toString()});
    // dio.options.method = method;
    // dio.options.baseUrl = BASE_URL;

    try {
      final result = await dio.request(url,
          queryParameters: params, options: options, data: data);
      String token = result.headers.value("token");
      if (token != null) {
        StorageUtil.setStringItem("token", token);
      }
      return result;
    } on DioError catch (err) {
      print("err:$err");
      throw err;
    }
  }

  // 3、封装下载
  void upLoad() {}
}
